package com.hpf.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hpf.lease.common.config.CommonException;
import com.hpf.lease.common.result.ResultCodeEnum;
import com.hpf.lease.model.bean.LeaseAgreement;
import com.hpf.lease.web.admin.dto.AgreementSearchSubmitDto;
import com.hpf.lease.web.admin.mapper.LeaseAgreementMapper;
import com.hpf.lease.web.admin.service.LeaseAgreementService;
import com.hpf.lease.web.admin.vo.LeaseAgreementInfoVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author GOD
* @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
* @createDate 2024-08-29 15:00:50
*/
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
    implements LeaseAgreementService {

    @Override
    @Transactional
    public boolean saveOrUpdateInfo(LeaseAgreement leaseAgreement) {
        long id = leaseAgreement.getId() == null ? -1L : leaseAgreement.getId();
        LeaseAgreement one = this.getOne(Wrappers.lambdaQuery(LeaseAgreement.class)
                .eq(id != -1L, LeaseAgreement::getId, id)
                .eq(LeaseAgreement::getApartmentId, leaseAgreement.getApartmentId())
                .eq(LeaseAgreement::getRoomId, leaseAgreement.getRoomId()));
        if (one == null || one.getId().equals(id) ||
             (one.getApartmentId().equals(leaseAgreement.getApartmentId()) && one.getRoomId().equals(leaseAgreement.getRoomId())) ){
            this.saveOrUpdate(leaseAgreement);
        }else {
            throw new CommonException(ResultCodeEnum.LEASE_AGREEMENT_ERROR);
        }
        return true;
    }

    @Override
    public LeaseAgreementInfoVo getDetailById(long id) {
        return baseMapper.getDetailById(id);
    }

    @Override
    public Page<LeaseAgreementInfoVo> getPage(AgreementSearchSubmitDto submitDto) {
        Page<LeaseAgreementInfoVo> page = new Page<>(submitDto.getCurrent(), submitDto.getSize());
        List<LeaseAgreementInfoVo> agreementInfoVo = baseMapper.getPageItem(page,submitDto);
        page.setRecords(agreementInfoVo);
        return page;
    }
}




